package db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;

import java.awt.Dimension;

import javax.swing.JSeparator;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class sale extends JPanel {
	private JTextField text;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;
	int index;
	DefaultTableModel model;
	Vector<String> col = new Vector<String>();
	Connection con;
	private JLabel label;
	private JSeparator separator;
	private JLabel label_1;
	private JTextField text1;
	private JTextField text2;
	private JTextField text4;
	private JTextField text3;
	private JTextField text5;
	private JTextField text6;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	private JLabel label_5;
	private JLabel label_6;
	private JLabel label_7;
	private JButton button;

	public sale(Connection c) {
		setBackground(Color.BLACK);
		this.con = c;
		col.add("제품코드");
		col.add("제품명");
		col.add("판매가");
		col.add("거래처");
		col.add("재고");
		model = new DefaultTableModel(col, 0);
		setLayout(null);
		add(getText());
		add(getBtnNewButton());
		add(getScrollPane());
		add(getLabel());
		add(getSeparator());
		add(getLabel_1());
		add(getText1());
		add(getText2());
		add(getText4());
		add(getText3());
		add(getTextField_2());
		add(getTextField_3());
		add(getLabel_2());
		add(getLabel_3());
		add(getLabel_4());
		add(getLabel_5());
		add(getLabel_6());
		add(getLabel_7());
		add(getButton());
		table.setRowHeight(25);
	}

	private boolean search2() {
		try {

			model.setRowCount(0);
			String str = text.getText();

			Statement stmt = con.createStatement();

			// 가져올 자료 요청
			String sql = "select c.pro_code,c.pro_nm,c.pro_price*1.2, cu.name, nvl(s.s_amount,0)"
					+ " from code c inner join customer cu "
					+ "on c.pro_cs_no=cu.num full outer join stock s "
					+ "on s.s_code=c.pro_code where c.pro_code like '%"
					+ str + "%' or c.pro_nm like '%" +str +"%' or cu.name like '%" + str + "%'";
			
			
			
			ResultSet RS = stmt.executeQuery(sql);

			while (RS.next()) {

				Vector<String> row = new Vector<String>();
				row.add(RS.getString(1));
				row.add(RS.getString(2));
				row.add(RS.getString(3));
				row.add(RS.getString(4));
				row.add(RS.getString(5));
				model.addRow(row);

			}
			RS.close();
			stmt.close();
			return true;
		} catch (Exception e) {
			System.out.println("예외사항:" + e.getMessage());
			return false;
		}
	}

	private JTextField getText() {
		if (text == null) {
			text = new JTextField();
			text.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
						search2();
					}
				}
			});
			text.setBounds(12, 71, 247, 21);
			text.setColumns(10);
		}
		return text;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("조 회");
			btnNewButton.setForeground(Color.WHITE);
			btnNewButton.setBackground(Color.BLACK);
			btnNewButton.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 30));
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					search2();
				}
			});
			btnNewButton.setBounds(271, 70, 97, 23);
		}
		return btnNewButton;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 102, 357, 286);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	public void MouseC() {
		index = table.getSelectedRow();  // 선택된 열의 table index번호를 저장

		text1.setText(table.getValueAt(index, 0) + "");  // 제품코드
		text2.setText(table.getValueAt(index, 1) + "");  // 제품명
		text4.setText(table.getValueAt(index, 2) + "");  // 판매가
		text5.setText(table.getValueAt(index, 3) + "");  // 거래처
		text6.setText(table.getValueAt(index, 4) + "");  // 재고

	}

	private JTable getTable() {
		if (table == null) {
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					MouseC();

				}
			});
		}
		return table;
	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("판 매");
			label.setPreferredSize(new Dimension(700, 25));
			label.setHorizontalAlignment(SwingConstants.CENTER);
			label.setForeground(Color.WHITE);
			label.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 50));
			label.setBounds(0, 10, 679, 30);
		}
		return label;
	}

	private JSeparator getSeparator() {
		if (separator == null) {
			separator = new JSeparator();
			separator.setPreferredSize(new Dimension(380, 5));
			separator.setBounds(22, 51, 679, 5);
		}
		return separator;
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("중앙HTA 할인매장 - 5조");
			label_1.setForeground(Color.WHITE);
			label_1.setFont(new Font("나눔손글씨 펜", Font.BOLD, 25));
			label_1.setBounds(474, 455, 218, 28);
		}
		return label_1;
	}

	private JTextField getText1() {
		if (text1 == null) {
			text1 = new JTextField();
			text1.setPreferredSize(new Dimension(6, 25));
			text1.setHorizontalAlignment(SwingConstants.CENTER);
			text1.setEditable(false);
			text1.setFont(new Font("맑은 고딕", Font.BOLD, 16));
			text1.setBounds(467, 123, 163, 30);
			text1.setColumns(12);
		}
		return text1;
	}

	private JTextField getText2() {
		if (text2 == null) {
			text2 = new JTextField();
			text2.setHorizontalAlignment(SwingConstants.CENTER);
			text2.setEditable(false);
			text2.setFont(new Font("맑은 고딕", Font.BOLD, 16));
			text2.setBounds(467, 164, 163, 30);
			text2.setColumns(12);
		}
		return text2;
	}

	private boolean sale() {
		try {
			SalStock ss=new SalStock(); 
			Statement stmt = con.createStatement();
			String code = text1.getText();
			String cname = text2.getText();
			int price = Integer.parseInt(text4.getText());
			int ea = Integer.parseInt(text3.getText());
			int tot = Integer.parseInt(text6.getText());
			ss.buy(code, ea);
			// 가져올 자료 요청
			String sql = "insert into sale values(sa_no.nextval, '" + code
					+ "'," + ea + ", " + price * ea
					+ ", '0', sysdate, 'A16', sysdate, 'A16')";

			ResultSet RS = stmt.executeQuery(sql);
			model.setValueAt(tot - ea, index, 4);
			JOptionPane.showMessageDialog(null, "제품판매완료");
			RS.close();
			stmt.close();
			
			text1.setText("");
			text2.setText("");
			text3.setText("");
			text4.setText("");
			text5.setText("");
			text6.setText("");

			return true;
		} catch (Exception e) {
			System.out.println("예외사항:" + e.getMessage());
			return false;
		}
	}

	private JTextField getText4() {
		if (text4 == null) {
			text4 = new JTextField();
			text4.setHorizontalAlignment(SwingConstants.CENTER);
			text4.setEditable(false);
			text4.setFont(new Font("맑은 고딕", Font.BOLD, 16));
			text4.setColumns(12);
			text4.setBounds(467, 205, 163, 30);
		}
		return text4;
	}

	private JTextField getText3() {
		if (text3 == null) {
			text3 = new JTextField();
			text3.setSize(new Dimension(60, 50));
			text3.setMinimumSize(new Dimension(6, 25));
			text3.setPreferredSize(new Dimension(6, 25));
			text3.setHorizontalAlignment(SwingConstants.CENTER);
			text3.setFont(new Font("맑은 고딕", Font.BOLD, 16));
			text3.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					try {
						if (text3.getText().equals("")) {
							return;
						}
						int ea = Integer.parseInt(text6.getText());
						int sale = Integer.parseInt(text3.getText());
						if (sale > ea) {
							JOptionPane.showMessageDialog(null,
									"해당 제품의 제고가 부족합니다.");
							text3.setText("");
						} else if (sale <= 0) {
							JOptionPane.showMessageDialog(null,
									"판매 수량을 정확히 입력해주세요.");
							text3.setText("");
						}
					} catch (Exception ev) {
						JOptionPane.showMessageDialog(null,
								"판매 수량을 정확히 ^^ 입력해주세요.");
						text3.setText("");
						text3.requestFocus();
						return;
					}
				}

				@Override
				public void keyPressed(KeyEvent e) {
				}
			});
			text3.setColumns(15);
			text3.setBounds(467, 325, 163, 30);
		}
		return text3;
	}

	private JTextField getTextField_2() {
		if (text5 == null) {
			text5 = new JTextField();
			text5.setHorizontalAlignment(SwingConstants.CENTER);
			text5.setEditable(false);
			text5.setFont(new Font("맑은 고딕", Font.BOLD, 16));
			text5.setColumns(12);
			text5.setBounds(467, 245, 163, 30);
		}
		return text5;
	}

	private JTextField getTextField_3() {
		if (text6 == null) {
			text6 = new JTextField();
			text6.setHorizontalAlignment(SwingConstants.CENTER);
			text6.setEditable(false);
			text6.setFont(new Font("맑은 고딕", Font.BOLD, 16));
			text6.setColumns(12);
			text6.setBounds(467, 285, 163, 30);
		}
		return text6;
	}

	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("제품 코드");
			label_2.setHorizontalAlignment(SwingConstants.CENTER);
			label_2.setForeground(Color.WHITE);
			label_2.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_2.setBounds(381, 123, 70, 30);
		}
		return label_2;
	}

	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("제품명");
			label_3.setHorizontalAlignment(SwingConstants.CENTER);
			label_3.setForeground(Color.WHITE);
			label_3.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_3.setBounds(381, 164, 70, 30);
		}
		return label_3;
	}

	private JLabel getLabel_4() {
		if (label_4 == null) {
			label_4 = new JLabel("판매가");
			label_4.setHorizontalAlignment(SwingConstants.CENTER);
			label_4.setForeground(Color.WHITE);
			label_4.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_4.setBounds(381, 205, 70, 26);
		}
		return label_4;
	}

	private JLabel getLabel_5() {
		if (label_5 == null) {
			label_5 = new JLabel("거래처");
			label_5.setHorizontalAlignment(SwingConstants.CENTER);
			label_5.setForeground(Color.WHITE);
			label_5.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_5.setBounds(381, 245, 70, 30);
		}
		return label_5;
	}

	private JLabel getLabel_6() {
		if (label_6 == null) {
			label_6 = new JLabel("재고");
			label_6.setHorizontalAlignment(SwingConstants.CENTER);
			label_6.setForeground(Color.WHITE);
			label_6.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_6.setBounds(381, 285, 74, 30);
		}
		return label_6;
	}

	private JLabel getLabel_7() {
		if (label_7 == null) {
			label_7 = new JLabel("판매 수량");
			label_7.setHorizontalAlignment(SwingConstants.CENTER);
			label_7.setForeground(Color.WHITE);
			label_7.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 23));
			label_7.setBounds(381, 328, 74, 30);
		}
		return label_7;
	}

	private JButton getButton() {
		if (button == null) {
			button = new JButton("판 매");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					sale();
				}
			});
			button.setForeground(Color.WHITE);
			button.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 30));
			button.setBackground(Color.BLACK);
			button.setBounds(533, 365, 97, 23);
		}
		return button;
	}
}
